<?php
namespace app\work\controller;
use app\work\logic\XtLogic;

/**
 * 内部协同api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/29
 * Time: 15:21
 */
class Xt extends Base {

    public function __construct(){
        parent::__construct();
        $this->x_logic = new XtLogic();
    }

    /**
     * 新增内部协同
     * @return \think\response\Json
     */
    public function ajax_add_xt(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->x_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查新增内部协同规则
        $r = $this->x_logic->check_add_xt_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wrx_data = [];
        $wrx_data['openid'] = $u_detail['openid'];
        $wrx_data['name'] = trim($params['name']);
        $wrx_data['content'] = trim($params['content']);
        $wrx_data['out_time'] = $out_time = date('Y-m-d H:i:s',strtotime('+12 hour'));
        $wrx_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
        $wrxid = model('work_record_xt')->insertGetId($wrx_data);
        if($wrxid){
            //生成事项（未开始）
            $m = new \app\common\controller\Matter();
            $r = $m->gen_matter_not($u_detail['openid'],$wrxid,2,$out_time,$params['deal_openid'],0);

            //生成事项（打开）
            $r = $m->save_matter_open($u_detail['openid'],$wrxid,2);

            //生成抄送
            $c = new \app\common\controller\Copy();
            if(!empty($params['copy_openid'])){
                //生成抄送（未抄送）
                $r = $c->gen_copy_not($u_detail['openid'],$wrxid,2,$params['copy_openid']);
                //修改抄送（已抄送）
                $r = $c->save_copy_open($u_detail['openid'],$wrxid,2);
            }

            return json($this->x_logic->get_response(1,'提交成功'));
        }
        return json($this->x_logic->get_response(1,'提交失败'));
    }
}
